//
//  AddressBook.m
//  AddressBook
//
//  Created by 小爪乎黑 on 14/11/29.
//  Copyright (c) 2014年 李帅. All rights reserved.
//

#import "AddressBook.h"
#import "Person.h"
#import "NSString+Characters.h"

@implementation AddressBook

- (void)dealloc
{
    
    [_addressBook release];

    [super dealloc];

    
}

-(NSString *)description{

    
    return [NSString stringWithFormat:@"%@",_addressBook];
}


#pragma mark ---初始化
-(instancetype)init
{
    if (self = [super init]) {
        
        self.addressBook = [NSMutableDictionary dictionary];
        
    }
    return self;

}

#pragma mark 添加联系人
- (BOOL)addPerson:(Person *)aPerson
{
    
    //1.判断联系人姓名和电话是否为空
    BOOL nameFlag = (aPerson.name == nil) || ([aPerson.name isEqualToString:@""]);
    
    BOOL phoneNumFlag = (aPerson.phoneNum == nil) || ([aPerson.phoneNum isEqualToString:@""]);
    
    
    
    if (!nameFlag && !phoneNumFlag) {
    //如果都不为空
    //获取分组首字母
        
        NSString *firstChar = [aPerson.name uppercasePinYinFirstLetter];
        
        //判断数组中是否有该Key值
        if (self.addressBook[firstChar] == nil) {
            
            //添加
            NSMutableArray *tempArray = [NSMutableArray arrayWithObject:aPerson];
            
            [self.addressBook setObject:tempArray forKey:firstChar];
            
        }else{
            
            
            //判断重名
            for (Person *p in self.addressBook[firstChar]) {
                
                if ([p.name isEqualToString:aPerson.name]) {
                    return NO;
                }
                
                
            }
            
        
            [self.addressBook[firstChar] addObject:aPerson];
        }
        

        
        return YES;
        
    }else{
    //如果为空直接返回NO
    
        return NO;
    }
    

}


#pragma mark 根据人名删除联系人
- (BOOL)deletePersonWithName:(NSString*)personName
{

    //获取联系人分组
    NSString *firstName = [personName uppercasePinYinFirstLetter];
    
    
    if (self.addressBook[firstName] == nil) {
        return NO;
    }else{
        
        NSArray *tempArr =[NSArray arrayWithArray:self.addressBook[firstName]];
        
        BOOL reFlag = NO;
        
        
        for (Person * p in tempArr) {
            if ([p.name isEqualToString:personName]) {
                [self.addressBook[firstName] removeObject:p];
                reFlag = YES;
            }
        }
        

    
        
        return reFlag;
    
    
    }

}


#pragma mark 删除分组

- (BOOL)deleteGroup:(NSString *)gourpName
{

    if(self.addressBook[gourpName] == nil)
    {
        return NO;
    
    }else
    {
        [self.addressBook[gourpName] removeAllObjects];
        

        [self.addressBook removeObjectForKey:gourpName];
        
        
        return YES;
    
    }


}

#pragma mark 根据电话查找联系人
-(Person *)findPersonWithPhoneNum:(NSString *)aPhoneNum
{

    for (NSString *key in self.addressBook) {
        
        for (Person *p in self.addressBook[key]) {
            if ([p.phoneNum isEqualToString:aPhoneNum]) {
                return p;
            }
            
            
        }
        
    }

    return nil;

}

#pragma mark 根据性别查找练习人并排序
-(NSArray *)findPersonWithSex:(NSString *)aSex
{
    //数组(用来存放查找到的联系人)
    NSMutableArray *tempArr =[NSMutableArray array];
    
    //根据性别查找联系人
    for (NSString *key in self.addressBook) {
        for (Person *p  in self.addressBook[key]) {
            
            if ([p.sex isEqualToString:aSex]) {
                [tempArr addObject:p];
            }
            
        }
    }
    
    //排序
    [tempArr sortUsingComparator:^NSComparisonResult(Person *obj1, Person *obj2) {
        
        if (obj1.age > obj2.age) {
            return NSOrderedDescending;
        }else if(obj1.age == obj2.age)
        {
            return NSOrderedSame;
        }else{
            return NSOrderedAscending;
        }
        
    }];
    
    
    
  
    
    return tempArr;


}



#pragma mark 修改
-(BOOL)changePersonWithName:(NSString *)name phoneNum:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger) aAge
{
    
    
    //获取联系人分组
    NSString *firstName = [name uppercasePinYinFirstLetter];
    
    
    for (Person *p in self.addressBook[firstName]) {
        
        if ([p.name isEqualToString:name]) {
            p.age = aAge;
            p.phoneNum = aNum;
            p.sex = aSex;
            return YES;
            
        }
        
    }

    return NO;

}


#pragma mark 展示所有联系人
-(void)showAll
{
    
    
    for (NSString *key in self.addressBook) {
        NSLog(@"%@",key);
        for (Person *p in self.addressBook[key]) {
            NSLog(@"%@",p);
        }
        
    }


}



@end
